package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.system.domain.SysLuck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysBank;
import com.ruoyi.project.system.service.ISysBankService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 银行人员Controller
 *
 * @author theme
 * @date 2021-04-20
 */
@Api(description = "银行人员Controller" )
@RestController
@RequestMapping("/system/bank" )
public class SysBankController extends BaseController {
    @Autowired
    private ISysBankService sysBankService;

/**
 * 查询银行人员列表
 */
@ApiOperation("查询银行人员列表" )
@PreAuthorize("@ss.hasPermi('system:bank:list')" )
@GetMapping("/list" )
        public TableDataInfo list(SysBank sysBank) {
        startPage();
        List<SysBank> list = sysBankService.selectSysBankList(sysBank);
        return getDataTable(list);
    }

    /**
     * 获取银行人员详细信息
     */
    @ApiOperation("获取银行人员详细信息" )
    @PreAuthorize("@ss.hasPermi('system:bank:query')" )
    @GetMapping(value = "/{bankUserId}" )
    public AjaxResult getInfo(@PathVariable("bankUserId" ) Long bankUserId) {
        return AjaxResult.success(sysBankService.selectSysBankById(bankUserId));
    }

    /**
     * 新增银行人员
     */
    @ApiOperation("新增银行人员" )
    @PreAuthorize("@ss.hasPermi('system:bank:add')" )
    @Log(title = "银行人员" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBank sysBank) {
        if(StringUtils.isEmpty(sysBank.getPhone())||sysBank.getPhone().length()!=11){
            return AjaxResult.error("手机号格式错误!");
        }
        SysBank data = sysBankService.selectBankByPhone(sysBank.getPhone());
        if(null!=data){
            return AjaxResult.error("手机号已存在!");
        }
        return toAjax(sysBankService.insertSysBank(sysBank));
    }

    /**
     * 修改银行人员
     */
    @ApiOperation("修改银行人员" )
    @PreAuthorize("@ss.hasPermi('system:bank:edit')" )
    @Log(title = "银行人员" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBank sysBank) {
        if(StringUtils.isEmpty(sysBank.getPhone())||sysBank.getPhone().length()!=11){
            return AjaxResult.error("手机号格式错误!");
        }
        SysBank data = sysBankService.selectBankByPhone(sysBank.getPhone());
        if(null!=data&&data.getBankUserId().longValue()!=sysBank.getBankUserId().longValue()){
            return AjaxResult.error("手机号已存在!");
        }
        return toAjax(sysBankService.updateSysBank(sysBank));
    }

    /**
     * 删除银行人员
     */
    @ApiOperation("删除银行人员" )
    @PreAuthorize("@ss.hasPermi('system:bank:remove')" )
    @Log(title = "银行人员" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{bankUserIds}" )
    public AjaxResult remove(@PathVariable Long[] bankUserIds) {
        return toAjax(sysBankService.deleteSysBankByIds(bankUserIds));
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, HttpServletRequest httpServletRequest) throws Exception {
        String areaName=httpServletRequest.getParameter("areaName");
        ExcelUtil<SysBank> util = new ExcelUtil<SysBank>(SysBank.class);
        List<SysBank> userList = util.importExcel(file.getInputStream());
        String message = sysBankService.importBank(userList,areaName);
        return AjaxResult.success(message);
    }
}
